package com.whalehearing.tingyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whalehearing.tingyou.common.core.domain.AjaxResult;
import com.whalehearing.tingyou.domain.HearingSyncUserProfile;
import com.whalehearing.tingyou.mapper.HearingSyncUserProfileMapper;
import com.whalehearing.tingyou.service.IHearingSyncUserProfileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 测前初评同步个人资料Service业务层处理
 *
 * @author zengjun-qxm
 * @date 2024-09-29
 */
@Service
public class HearingSyncUserProfileServiceImpl extends ServiceImpl<HearingSyncUserProfileMapper, HearingSyncUserProfile> implements IHearingSyncUserProfileService {

    @Autowired
    private HearingSyncUserProfileMapper syncUserProfileMapper;


    /**
     * 根据-userId-获取测前初评同步个人资料详细信息
     */
    @Override
    public HearingSyncUserProfile getSpecialByUserId(Long userId) {
        LambdaQueryWrapper<HearingSyncUserProfile> queryWrapper = new LambdaQueryWrapper<HearingSyncUserProfile>().eq(HearingSyncUserProfile::getIsDeleted, 0).eq(HearingSyncUserProfile::getUserId, userId);
        return syncUserProfileMapper.selectOne(queryWrapper);
    }

    /**
     * 新增测前初评同步个人资料
     */
    @Override
    public AjaxResult addAndUpdate(HearingSyncUserProfile hearingSyncUserProfile) {
        LambdaQueryWrapper<HearingSyncUserProfile> wrapper = new LambdaQueryWrapper<HearingSyncUserProfile>().eq(HearingSyncUserProfile::getUserId, hearingSyncUserProfile.getUserId());
        HearingSyncUserProfile exist = syncUserProfileMapper.selectOne(wrapper);
        //查询用户
        if (exist == null) {
            //该用户不存在-新增逻辑
            save(hearingSyncUserProfile);
            return AjaxResult.success();
        } else {
            //该用户已存在-修改逻辑
            hearingSyncUserProfile.setSyncUserProfileId(exist.getSyncUserProfileId());
            updateById(hearingSyncUserProfile);
            return AjaxResult.success();
        }
    }


}
